import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
//引入个人路由
import letao from './乐涛/index'
import lijialong from './李嘉龙/index'
import zhaokuangtao from './赵匡涛/index'
import tangbin from './唐斌/index'
import liujunjie from './刘俊杰/index'

Vue.use(VueRouter)
const routes = [
  {
    path: '/test',
    component: () => import('@/views/乐涛/Dashboard/Test.vue')
  },
  // {
  //   path: '/Login',
  //   component: () => import('@/views/赵匡涛/Login/Login.vue')
  // },
  ...letao,
  ...lijialong,
  ...zhaokuangtao,
  ...tangbin,
  ...liujunjie,
  {
    path: '/error',
    component: () => import('@/views/Error/Error.vue')//无权限界面
  },
  {
    path: '*',
    component: () => import('@/views/Error/NoPath.vue'),//去页面丢失的提示界面
    meta: {
      title: '页面错误'
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: 'ActiveCls'
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path.toUpperCase() != '/LOGIN' && to.path.toUpperCase() != '/ERROR') {
    //判断是否登陆
    if (Object.keys(router.app.$options.store.state.UserInfo).length) {
      let rt = router.app.$options.store.state.RouterDt
      //console.log(rt)

      if (rt.length == 0) {
        //alert(rt.length)
        axios({
          url: '/HrApi/MenuMange/GetUserMenuList',
          method: 'GET',
          params: {
            pk_User: router.app.$options.store.state.UserInfo.pk_User
          }
        }).then(r => {
          rt = r.data.data

          if (JSON.stringify(rt).indexOf(to.path) > -1) {
            //this.$store.dispatch('SetUserInfo', {})
            router.app.$options.store.commit('SetRouterDt', r.data.data)
            next()
          } else {
            router.app.$options.store.commit('SetUserInfo', {})
            router.app.$options.store.commit('SetRouterDt', [])
            next('/error');
          }
        })
      } else {
        if (JSON.stringify(rt).indexOf(to.path) > -1) {
          //this.$store.dispatch('SetUserInfo', {})
          next()
        } else {
          next('/error');
          router.app.$options.store.commit('SetUserInfo', {})
          router.app.$options.store.commit('SetRouterDt', [])

        }
      }
    } else {
      //重定向
      next("/login");
    }

  } else {
    router.app.$options.store.commit('SetUserInfo', {})
    router.app.$options.store.commit('SetRouterDt', [])
    //重定向
    next()
  }
  // if (to.matched.some(res => res.meta.isLogin)) {//判断是否需要登录
  //   if (sessionStorage['username']) {
  //     next();
  //   } else {
  //     next({
  //       path: "/login",
  //       query: {
  //         redirect: to.fullPath
  //       }
  //     });
  //   }

  // } else {
  //   next()
  // }
});

function filterAsyncRouter (rt) {
  // 遍历后台传来的路由字符串，转换为组件对象
  let accessedRouters = rt.filter(r => {
    // if (router.meta) {
    //   // 默认图标处理
    //   router.meta.icon = router.meta.icon ? router.meta.icon : "component";
    // }
    // if (router.component === "main") {
    //   // Main组件特殊处理
    //   router.component = AppMain;
    // }
    //else {
    //处理组件---重点
    r.component = loadView(r.component);
    // alert(router.component)
    //}
    //存在子集
    if (r.children && r.children.length) {
      r.children = filterAsyncRouter(r.children);
    }
    return true;
  });
  return accessedRouters;
}
function loadView (view) {
  // 路由懒加载
  //alert(view)
  return () => import(`@/views/${view}`);
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
